﻿Public Class ctrPhanCongNV

    Private Sub ctrPhanCongNV_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gvBan_Load()
        gvNhanVien_Load()
        gvPhanCong_Load()
    End Sub

    Private Sub gvNhanVien_Load()
        Dim str As String = "select nv.MaNV, HoTen, GioiTinh, count(*) as SoBanPhuTrach" _
                            & " from NHANVIEN NV, DSPHANCONG PC, CHUCVU CV" _
                            & " where TinhTrang=True and TenCV='Phục vụ' and PC.MaNV = NV.MaNV and NV.MaCV = CV.MaCV" _
                            & " group by nv.MaNV, HoTen, GioiTinh"
        Dim bus As GeneralBUS = New GeneralBUS()
        gvNhanVien.DataSource = bus.GetTable(str)
    End Sub

    Private Sub gvBan_Load()
        If gvNhanVien.CurrentRow IsNot Nothing Then
            Dim str As String = "select * from BAN where MaBan not in (select MABAN from DSPHANCONG)"
            Dim bus As GeneralBUS = New GeneralBUS()
            gvBan.DataSource = bus.GetTable(str)
        End If
    End Sub

    Private Sub gvPhanCong_Load()
        Dim bus As DSPhanCongBUS = New DSPhanCongBUS()
        gvPhanCong.DataSource = bus.GetTable()
    End Sub

    Private Sub gvBan_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvBan.DoubleClick
        Dim tmp As DSPhanCongDTO = New DSPhanCongDTO()
        tmp.MaBan = gvBan.CurrentRow.Cells(0).Value.ToString()
        tmp.MaNV = gvNhanVien.CurrentRow.Cells(0).Value.ToString()

        Dim bus As DSPhanCongBUS = New DSPhanCongBUS()
        bus.Insert(tmp)

        gvBan_Load()
        gvNhanVien_Load()
        gvPhanCong_Load()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim tmp As DSPhanCongDTO = New DSPhanCongDTO()
        tmp.MaNV = gvPhanCong.CurrentRow.Cells(0).Value.ToString()
        tmp.MaBan = gvPhanCong.CurrentRow.Cells(1).Value.ToString()

        Dim bus As DSPhanCongBUS = New DSPhanCongBUS()
        bus.Delete(tmp)

        gvBan_Load()
        gvNhanVien_Load()
        gvPhanCong_Load()
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        frmMain.TabControl1.TabPages.Remove(frmMain.TabControl1.SelectedTab)
    End Sub

    Private Sub gvNhanVien_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvNhanVien.SelectionChanged
        gvBan_Load()
    End Sub
End Class
